import Fruit.Fruit;
import Fruit.impl.Apple;
import Fruit.impl.Mango;
import Fruit.impl.Strawberry;
import Strategy.Strategy;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {

        // 问题1: 现在顾客 A 在超市购买了若干斤苹果和草莓，需要计算一共多少钱？
        try {
            Apple apple1 = new Apple(2);
            Strawberry strawberry1 = new Strawberry( 2);

            List<Fruit> fruits1 = new ArrayList<>();
            fruits1.add(apple1);
            fruits1.add(strawberry1);

            //使用默认策略
            Order.Order order1 = new Order.Order(fruits1);
            System.out.println(order1.computeSum());
        } catch (Exception e){
            System.out.println(e.getMessage());
        }

        // 问题2: 超市增加了一种水果芒果，其定价为 20 元/斤。
        //现在顾客 B 在超市购买了若干斤苹果、 草莓和芒果，需计算一共需要多少钱？
        try {
            Apple apple2 = new Apple(2);
            Strawberry strawberry2 = new Strawberry( 2);
            Mango mango2 = new Mango( 2);

            List<Fruit> fruits2 = new ArrayList<>();
            fruits2.add(apple2);
            fruits2.add(strawberry2);
            fruits2.add(mango2);

            //使用默认策略
            Order.Order order2 = new Order.Order(fruits2);
            System.out.println(order2.computeSum());
        } catch (Exception e){
            System.out.println(e.getMessage());
        }

        // 问题3:超市做促销活动，草莓限时打 8 折。
        //现在顾客 C 在超市购买了若干斤苹果、 草莓和芒果，需计算一共需要多少钱？
        try {
            Apple apple3 = new Apple(2);
            Strawberry strawberry3 = new Strawberry( 2);
            Mango mango3 = new Mango( 2);

            List<Fruit> fruits3 = new ArrayList<>();
            fruits3.add(apple3);
            fruits3.add(strawberry3);
            fruits3.add(mango3);

            //配置超市做促销活动，草莓限时打 8 折
            Strategy strategy3 = new Strategy() {
                @Override
                public double compute(List<Fruit> fruits) {
                    double sum = 0.0;
                    for(Fruit fruit : fruits) {
                        if(fruit instanceof Strawberry || "草莓".equals(fruit.getName())) {
                            sum+= 0.8 * fruit.getUnitPrice() * fruit.getWeight();
                        } else {
                            sum+= fruit.getUnitPrice() * fruit.getWeight();
                        }
                    }
                    return sum;
                }
            };

            Order.Order order3 = new Order.Order(fruits3, strategy3);
            System.out.println(order3.computeSum());
        } catch (Exception e){
            System.out.println(e.getMessage());
        }

        // 问题4:促销活动效果明显，超市继续加大促销力度，购物满 100 减 10 块。
        //现在顾客 D 在超市购买了若干斤苹果、 草莓和芒果，需计算一共需要多少钱？
        try {
            Apple apple4 = new Apple(8);
            Strawberry strawberry4 = new Strawberry( 8);
            Mango mango4 = new Mango( 8);

            List<Fruit> fruits4 = new ArrayList<>();
            fruits4.add(apple4);
            fruits4.add(strawberry4);
            fruits4.add(mango4);

            //配置购物满 100 减 10 块策略
            Strategy strategy4 = new Strategy() {
                @Override
                public double compute(List<Fruit> fruits) {
                    double sum = super.compute(fruits);
                    int discountCount = (int) (sum / 100);
                    sum -= discountCount * 10;
                    return sum;
                }
            };

            Order.Order order4 = new Order.Order(fruits4, strategy4);
            System.out.println(order4.computeSum());
        } catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
}